package com.yonyou.brigade.basic_init.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.ybzboot.starter.facade.result.FacadeResult;
import com.yonyou.ybzboot.sysconfig.thread.InvocationInfoSetter;
import com.yonyou.ybzboot.tenant.client.TenantClient;
import com.yonyou.ybzboot.uc.client.UcUserClient;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户中心服务封装
 *
 * @author admin
 */
@Service
public class UserCenterServiceImpl implements IUserCenterService {
    @Autowired
    private UcUserClient ucUserClient;

    @Autowired
    private TenantClient tenantClient;

    private Logger logger = LoggerFactory.getLogger(UserCenterServiceImpl.class);

    @Override
    public List<UcUserDto> getUcUserDtoByOrg(String tenant, String orgPk) {
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        logger.info("开始调用用户中心listUcUserByOrgPk，tenant:" + tenant + "orgPk:" + orgPk);
        FacadeResult<List<UcUserDto>> result = ucUserClient.listUcUserByOrgPk(orgPk, tenant);
        if (result != null) {
            logger.info("listUcUserByOrgPk，tenant:" + tenant + "orgPk:" + orgPk + "返回结果：" + result.toString());
        } else {
            logger.error("listUcUserByOrgPk调用远程接口返回为null," + tenant + "," + orgPk);
        }
        if (result != null && result.isSuccess()) {
            ucUserDtos = result.getData();
        } else {
            logger.error("返回状态值错误，方法listUcUserByOrgPk参数tenant:" + tenant + ",orgPk" + orgPk);
        }
        return ucUserDtos;
    }

    public UcUserDto getCompleteUcUserDtoByUserId(String userId, String tenant) {
        List<String> userIds = new ArrayList<>();
        UcUserDto userDto = null;
        userIds.add(userId);
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        FacadeResult<List<UcUserDto>> result = ucUserClient.listCompleteUcUserByUserIds(userIds, tenant);
        if (result != null) {
            logger.info(result.toString());
        } else {
            logger.error("listCompleteUcUserByUserIds调用远程接口返回为null," + tenant + "," + userId);
        }
        if (result != null && result.isSuccess()) {
            ucUserDtos = result.getData();
            if (ucUserDtos != null && ucUserDtos.size() > 0) {
                userDto = ucUserDtos.get(0);
            }
        } else {
            logger.error("返回状态值错误，方法getCompleteUcUserDtoByUserId参数tenant:" + tenant + ",userId" + userId);
        }
        return userDto;
    }

    @Override
    public UcUserDto getCompleteUcUserDtoByPhone(String phone, String tenant) {
        UcUserDto ucUserDto = null;
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        List<String> phones = new ArrayList<>();
        phones.add(phone);
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        FacadeResult<List<UcUserDto>> result = ucUserClient.listCompleteUcUserByUserMobiles(phones, tenant);
        if (result != null) {
            logger.info(result.toString());
        } else {
            logger.error("listCompleteUcUserByUserMobiles调用远程接口返回为null," + tenant + "," + phone);
        }

        if (result != null && result.isSuccess()) {
            ucUserDtos = result.getData();
        } else {
            logger.error("返回状态值错误，方法getUcUserDtoByPhone参数tenant:" + tenant + ",phone" + phone + result.getMsg());
        }
        if (ucUserDtos != null && ucUserDtos.size() > 0) {
            ucUserDto = ucUserDtos.get(0);
        }
        return ucUserDto;

    }


    /**
     * 查询带有离职信息的用户同步
     *
     * @param phone
     * @param tenant
     * @return
     */
    @Override
    public UcUserDto listCompleteUcUserByUserMobiles4admin(String phone, String tenant) {
        UcUserDto ucUserDto = null;
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        List<String> phones = new ArrayList<>();
        phones.add(phone);
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        FacadeResult<List<UcUserDto>> result = ucUserClient.listCompleteUcUserByUserMobiles4admin(phones, tenant);
        if (result != null) {
            logger.info("listCompleteUcUserByUserMobiles4admin:" + result.toString());
        } else {
            logger.error("listCompleteUcUserByUserMobiles4admin调用远程接口返回为null," + tenant + "," + phone);
        }

        if (result != null && result.isSuccess()) {
            ucUserDtos = result.getData();
        } else {
            logger.error("返回状态值错误，方法listCompleteUcUserByUserMobiles4admin参数tenant:" + tenant + ",phone" + phone + result.getMsg());
        }
        if (ucUserDtos != null && ucUserDtos.size() > 0) {
            ucUserDto = ucUserDtos.get(0);
        }
        return ucUserDto;

    }


    /**
     * 根据userid和租户ID获取用户中心的完整用户信息
     *
     * @param userIdList
     * @param tenant
     * @return
     */
    @Override
    public List<UcUserDto> getCompleteUcUserDtoByUserid(List<String> userIdList, String tenant) {
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        logger.info("根据租户ID和用户ID数组，从用户中心查用户信息");
        logger.info("入参 tenant=" + tenant + "; userIdList=" + JSON.toJSONString(userIdList));
        FacadeResult<List<UcUserDto>> listFacadeResult = ucUserClient.listCompleteUcUserByUserIds(userIdList, tenant);
        if (listFacadeResult != null) {
            logger.info(listFacadeResult.toString());
        } else {
            logger.error("listCompleteUcUserByUserIds调用远程接口返回为null," + tenant + "," + userIdList.toString());
        }
        logger.info("返回值 listFacadeResult=" + JSON.toJSONString(listFacadeResult));


        if (listFacadeResult != null && listFacadeResult.isSuccess()) {
            return listFacadeResult.getData();
        }
        return null;
    }

    /**
     * 根据pcode和租户ID获取用户中心的完整用户信息
     *
     * @param personCode 用户编码，如用友员工编号0000078940
     * @param tenant     租户ID
     * @Return: List<UcUserDto>
     * @Date: 2018/9/13 14:38
     */
    @Override
    public UcUserDto getCompleteUcUserDtoByPersonCode(String personCode, String tenant) {
        UcUserDto ucUserDto = null;
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        List<String> personCodes = new ArrayList<>();
        personCodes.add(personCode);
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        FacadeResult<List<UcUserDto>> listFacadeResult = ucUserClient.listCompleteUcUserByPersonCodes(personCodes, tenant);
        if (listFacadeResult != null) {
            logger.info(listFacadeResult.toString());
        } else {
            logger.error("listCompleteUcUserByPersonCodes调用远程接口返回为null," + tenant + "," + personCode.toString());
        }
        if (listFacadeResult != null && listFacadeResult.isSuccess()) {
            ucUserDtos = listFacadeResult.getData();
        } else {
            logger.error("返回状态值错误，方法getCompleteUcUserDtoByPersonCode参数tenant:" + tenant + ",personCode" + personCode + listFacadeResult.getMsg());
        }
        if (ucUserDtos != null && ucUserDtos.size() > 0) {
            ucUserDto = ucUserDtos.get(0);
        }
        return ucUserDto;
    }

    public List<UcUserDto> getCompleteUcUserDtosByPhones(List<String> phones, String tenant) {
        InvocationInfoSetter.setInvocationInfo4rpc(tenant);
        FacadeResult<List<UcUserDto>> result = ucUserClient.listCompleteUcUserByUserMobiles(phones, tenant);
        List<UcUserDto> ucUserDtos = new ArrayList<>();
        if (result != null) {
            logger.info(result.toString());
        } else {
            logger.error("listCompleteUcUserByUserMobiles调用远程接口返回为null," + tenant + "," + phones.toString());
        }
        if (result != null && result.isSuccess()) {
            ucUserDtos = result.getData();
        } else {
            logger.error("返回状态值错误，方法getUcUserDtosByPhones参数tenant:" + tenant + ",phones:" + phones.toString());
        }
        return ucUserDtos;
    }

    @Override
    public List<UcUserDto> getCompleteUcUserByPersonIds(List<String> personIds, String tenantId) {
        InvocationInfoSetter.setInvocationInfo4rpc(tenantId);
        logger.info("根据租户ID和人员pk数组，从用户中心查用户信息");
        logger.info("入参 tenant=" + tenantId + "; userIdList=" + JSON.toJSONString(personIds));
        FacadeResult<List<UcUserDto>> listFacadeResult = ucUserClient.listCompleteUcUserByPersonIds(personIds, tenantId);
        if (listFacadeResult != null) {
            logger.info(listFacadeResult.toString());
        } else {
            logger.error("listCompleteUcUserByPersonIds调用远程接口返回为null," + tenantId + "," + personIds.toString());
        }
        logger.info("返回值 listFacadeResult=" + JSON.toJSONString(listFacadeResult));

        if (listFacadeResult != null && listFacadeResult.isSuccess()) {
            return listFacadeResult.getData();
        }
        return null;
    }

    /**
     * 根据租户ID查询租户信息
     *
     * @param tenantId 租户ID
     * @return JSONObject
     */
    @Override
    public JSONObject getTenantInfoById(String tenantId) {
        JSONObject result = null;
        logger.info("开始调用用户中心queryByTenantId，tenant:" + tenantId);
        FacadeResult<JSONObject> jsonObjectFacadeResult = tenantClient.queryByTenantId(tenantId);
        if (jsonObjectFacadeResult != null && jsonObjectFacadeResult.isSuccess()) {
            result = jsonObjectFacadeResult.getData();
            JSONObject tenant = result.getJSONObject("tenant");
            tenant.remove("privatekey");
            tenant.remove("carrierkey");
            result.put("tenant", tenant);
        } else {
            logger.error("返回状态值错误，方法queryByTenantId参数tenant:" + tenantId);
        }
        return result;
    }

    @Override
    public List<UcUserDto> getUcUsersByTenantId(String tenantId) {
        InvocationInfoSetter.setInvocationInfo4rpc(tenantId);
        logger.info("根据租户ID，从用户中心查用户信息");
        logger.info("入参 tenant=" + tenantId);
        FacadeResult<List<UcUserDto>> listFacadeResult = ucUserClient.listByTenantId(tenantId);
        if (listFacadeResult != null) {
            logger.info(listFacadeResult.toString());
        } else {
            logger.error("listByTenantId调用远程接口返回为null," + tenantId);
        }
        logger.info("返回值 listFacadeResult=" + JSON.toJSONString(listFacadeResult));

        if (listFacadeResult != null && listFacadeResult.isSuccess()) {
            return listFacadeResult.getData();
        }
        return null;
    }
}
